<template>
	<div class="system-auth-edit-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" :close-on-click-modal="false">
			<el-row :gutter="35" v-loading="state.loading">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-tree ref="authTree" :data="state.menuData" :props="state.menuProps" node-key="id" :check-strictly="true" :default-checked-keys="state.checkData" default-expand-all show-checkbox class="menu-data-tree" />
				</el-col>
			</el-row>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="editAuthDialog">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { roleApi } from '/@/api/role/index';
const authTree = ref();
// 定义变量内容
const authEditDialogFormRef = ref();
const state = reactive({
	roleid:'',
	loading:false,
	menuData: [],
	checkData:[],
	menuProps: {
		children: 'children',
		label: 'title',
	},
	dialog: {
		isShowDialog: false,
		title: '修改权限',
		submitTxt: '修改',
	},
});

// 打开弹窗
const openDialog = (row) => {
	state.roleid = row.id;
	state.dialog.isShowDialog = true;
	state.menuData = [],
	state.checkData = [],
	getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	let tree = authTree.value.getCheckedKeys()
	roleApi().setRoles({id:state.roleid,node:tree}).then(res=>{
		closeDialog();
		ElMessage.success('修改成功');
	})
};
// 获取菜单结构数据
const getMenuData = () => {
	state.loading = true;
	roleApi().roles({id:state.roleid}).then(res=>{
		state.loading = false;
		state.menuData = res.data.roleList
		state.checkData = res.data.select
		console.log(state.checkData );
	})
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-auth-edit-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
